package org.modernTeleport;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.Objects;
import javax.annotation.ParametersAreNonnullByDefault;

public class TpaCommand implements TabExecutor {
    ModernTeleport plugin;

    public TpaCommand(ModernTeleport plugin){
        this.plugin = plugin;
    }

    @Override
    @ParametersAreNonnullByDefault
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if(!(sender instanceof Player)){
            // 控制台无法使用
            plugin.SayToConsole(plugin.getConfig().getString("Console-Cant-Use"));
            return false;
        }
        Player Sender = (Player) sender;
        if(args.length < 2){
            // 至少需要两个参数
            // 即/tpa [go | here | refuse | accept | cancel | ignore | auto] <player> (accept|refuse)
            Sender.performCommand("tpahelp");
            return false;
        } else if (Bukkit.getPlayer(args[1]) == null) {
            // 如果tpa的玩家不存在
            Sender.sendMessage(plugin.getConfig().getString("Player-Not-Found"));
            return false;
        }
        Player Target = Objects.requireNonNull(Bukkit.getPlayer(args[1]));
        TeleportRequest request;
        switch (args[0]){
            case "go":
                // TODO: 是否允许一个玩家同时发出多个请求
                plugin.NewRequests.add(
                    new TeleportRequest(
                        Sender,
                        Target,
                        plugin.getConfig().getInt("RequestLifeTime"),
                        plugin.getConfig().getInt("TeleportDelay"),
                        RequestStatus.Go));
                Sender.sendMessage(
                        Objects.requireNonNull(plugin.getConfig().getString("Send-Go-Request"))
                            .replace("%player%", Target.getName())
                            .replace("%lifetime%",String.valueOf(plugin.getConfig().getInt("RequestLifeTime"))));
                Target.sendMessage(
                        Objects.requireNonNull(plugin.getConfig().getString("Receive-Go-Request"))
                            .replace("%player%", Sender.getName())
                            .replace("%lifetime%",String.valueOf(plugin.getConfig().getInt("RequestLifeTime"))));
                return true;
            case "come":
                plugin.NewRequests.add(
                new TeleportRequest(
                        Sender,
                        Target,
                        plugin.getConfig().getInt("RequestLifeTime"),
                        plugin.getConfig().getInt("TeleportDelay"),
                        RequestStatus.Come));
                Sender.sendMessage(
                        Objects.requireNonNull(plugin.getConfig().getString("Send-Come-Request"))
                                .replace("%player%", Target.getName())
                                .replace("%lifetime%",String.valueOf(plugin.getConfig().getInt("RequestLifeTime"))));
                Target.sendMessage(
                        Objects.requireNonNull(plugin.getConfig().getString("Receive-Come-Request"))
                                .replace("%player%", Sender.getName())
                                .replace("%lifetime%",String.valueOf(plugin.getConfig().getInt("RequestLifeTime"))));
                return true;
            case "accept":
                request = plugin.Requests.stream()
                        .filter(r -> Sender.getName().equals(r.target.getName()))
                        .filter(r -> Target.getName().equals(r.requester.getName()))
                        .findAny()
                        .orElse(null);
                if (request==null){
                    Sender.sendMessage(plugin.getConfig().getString("Request-Not-Found"));
                    return false;
                }
                request.Wait();
                int delay = plugin.getConfig().getInt("TeleportDelay");
                Sender.sendMessage(
                        Objects.requireNonNull(plugin.getConfig().getString(delay==0?"Accept-Request-Without-Delay":"Accept-Request"))
                                .replace("%delay%",String.valueOf(plugin.getConfig().getInt("TeleportDelay"))));
                Target.sendMessage(
                        Objects.requireNonNull(plugin.getConfig().getString(delay==0?"Request-Be-Accepted-Without-Delay":"Request-Be-Accepted"))
                            .replace("%delay%",String.valueOf(plugin.getConfig().getInt("TeleportDelay")))
                            .replace("%player%", Sender.getName()));
                return true;
            case "refuse":
                request = plugin.Requests.stream()
                        .filter(r -> Sender.getName().equals(r.target.getName()))
                        .filter(r -> Target.getName().equals(r.requester.getName()))
                        .findAny()
                        .orElse(null);
                if (request==null){
                    Sender.sendMessage(plugin.getConfig().getString("Request-Not-Found"));
                    return false;
                }
                request.Disable();
                Sender.sendMessage(
                        Objects.requireNonNull(plugin.getConfig().getString("Refuse-Request"))
                                .replace("%player%",Target.getName()));
                Target.sendMessage(
                        Objects.requireNonNull(plugin.getConfig().getString("Request-Be-Refused"))
                                .replace("%player%", Sender.getName()));
                return true;
            default:
                Sender.performCommand("tpahelp");
                return false;
        }
    }

    @Override
    @ParametersAreNonnullByDefault
    public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
        return List.of();
    }
}
